<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    {{info}}
    <input type="button" value="发出异步get请求" @click="f()">
    <input type="button" value="发出异步post请求" @click="f2()">
</div>
<!--引入axios和vue框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            info:"异步测试"
        },
        methods: {
            f(){
                //发出异步的get请求
                //参数写在请求地址的后面通过?连接,多个参数通过&分隔
                axios.get("/helloAxios?name=tom&age=18").then(function (response) {
                    // response代表响应对象,里面的data属性代表服务器响应的数据
                    // alert(response.data);
                    v.info = response.data;
                })
            },
            f2(){
                //发出异步的post请求, post请求参数是存放在请求体里面的
                //post请求时参数封装到JS对象里面
                let data = {name:"tom",age:18};
                axios.post("/postAxios",data).then(function (response) {
                    //把服务器响应的数据显示到页面中
                    v.info = response.data;
                })
            }
        }
    })
</script>
</body>
</html>